< previous page page_593 next page >

Page 593
routine to actually calculate the color. It passes CalculateHitColor the point of intersection, the surface normal at that point, and the sphere's shading model parameters.
Subroutine CalculateHitColor uses the shading model described in Chapter 16 to calculate the color of a pixel on an arbitrary object. For details on how this calculation works, see the code or look in Chapter 16.
e10a6a0a59f035e870d90cca05b85fce.gif
' Return the red, green, and blue components of 
' the surface at the hit position. 
Public Sub RayTraceable_FindHitColor( _ 
    ByVal depth As Integer, Objects As Collection, _ 
    ByVal eye_x As Single, ByVal eye_y As Single, _ 
    ByVal eye_z As Single, ByVal px As Single, _ 
    ByVal py As Single, ByVal pz As Single, _ 
    ByRef R As Integer, ByRef G As Integer, _ 
    ByRef B As Integer) 
Dim Nx As Single 
Dim Ny As Single 
Dim Nz As Single 
Dim n_len As Single 

    ' Find the unit normal at this point.
    Nx = px - Center.Trans(1)
    Ny = py - Center.Trans(2)
    Nz = pz - Center.Trans(3)
    n_len = Sqr(Nx * Nx + Ny * Ny + Nz * Nz)
    Nx = Nx / n_len
    Ny = Ny / n_len
    Nz = Nz / n_len

    ' Get the hit color.
    CalculateHitColor depth, Objects, Me, _
        eye_x, eye_y, eye_z, _
        px, py, pz, _ 
        Nx, Ny, Nz, _ 
        DiffuseKr, DiffuseKg, DiffuseKb, _
        AmbientKr, AmbientKg, AmbientKb, _
        SpecularK, SpecularN, _
        ReflectedKr, ReflectedKg, ReflectedKb, IsReflective, _
        TransmittedKr, TransmittedKg, TransmittedKb, TransN, _
        n1, n2, IsTransparent, _
        R, G, B
End Sub
Tracing Rays
Using the FindT and FindHitColor routines, a program can trace rays through a scene. The TraceRay subroutine shown in the following code traces a ray from the center of

 
< previous page page_593 next page >